<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>填写订单</title>
    <script th:src="@{/layui/layui.js}" type="text/javascript"></script>
    <link th:href="@{/layui/css/layui.css}" rel="stylesheet" />
</head>
<body>

<div style="margin: 20px">
    <form class="layui-form layui-form-pane">
        <input type="hidden" name="orderStatus" id="orderStatus">
        <input type="hidden" name="mid" id="mid" value="1">
        <div class="layui-form-item">
            <label class="layui-form-label">收货人姓名</label>
            <div class="layui-input-block">
                <input class="layui-input" name="consignee" lay-verify="required" th:value="${m.userName}" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电话号码</label>
            <div class="layui-input-block">
                <input class="layui-input" name="mobile" lay-verify="required|phone" th:value="${m.mobile}" autocomplete="off"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">收货地址</label>
            <div class="layui-input-block">
                <input class="layui-input" name="address" th:value="${a.province}+${a.city}+${a.area}+${a.detailAddress}" autocomplete="off" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注留言</label>
            <div class="layui-input-block">
                <input class="layui-input" name="message" autocomplete="off" />
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline"></div>
            <label class="layui-form-label">商品费用</label>
            <div class="layui-input-inline">
                <input class="layui-input" name="goodsPrice" id="goodsPrice" th:value="${g.currentPrice}" autocomplete="off" readonly/>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">购买数量</label>
                <div class="layui-input-inline">
                    <select class="layui-select" lay-filter="goodsNum" name="goodsNum" id="goodsNum" lay-verify="required" lay-search="" >
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">需支付</label>
            <div class="layui-input-block">
                <input class="layui-input" name="actualPrice" id="actualPrice" autocomplete="off" readonly/>
            </div>
        </div>
        <div class="layui-form-item" style="display: none;">
            <div class="layui-input-block">
                <a class="layui-btn" id="paySubmitBtn" lay-submit="" lay-filter="paySubmitBtn">交钱</a>
                <a class="layui-btn" id="cartSubmitBtn" lay-submit="" lay-filter="cartSubmitBtn">购物车</a>
            </div>
        </div>
    </form>
</div>

<!---->
<script>
    var $;
    layui.use(['form','jquery','layer'],function() {
        var layer = layui.layer,
            form = layui.form;
        $ = layui.jquery;


        var price = $('#goodsPrice').val();
        var num = $('#goodsNum').val();
        var total = price*num;
        $('#actualPrice').val(total);

        form.on('select(goodsNum)',function (obj) {
            var price = $('#goodsPrice').val();
            var num = $('#goodsNum').val();
            var total = price*num;
            $('#actualPrice').val(total);
        })

        form.on('submit(paySubmitBtn)',function (data) {
            var formData = data.field;
            formData.orderStatus = 0;
            formData.mid = $("#mid").val();//TODO 暂时没有登录
            $.ajax({
                type:'post',
                url:'/order/oc/add',
                data:formData,
                dataType:'json',
                success:function (result) {
                    if (result.is){
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);  //编辑成功后关闭父页面窗口
                        parent.layui.table.reload('memberTable');//根据表格id刷新父页面表格
                    }
                }
            })
        })

        form.on('submit(cartSubmitBtn)',function (data) {
            var formData = data.field;
            formData.orderStatus = 1;
            $.ajax({
                type:'post',
                url:'/order/oc/add',
                data:formData,
                dataType:'json',
                success:function (result) {
                    layer.msg("已存入购物车");
                    if (result.is){
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);  //编辑成功后关闭父页面窗口
                        parent.layui.table.reload('memberTable');//根据表格id刷新父页面表格
                    }
                }
            })
        })



    });


    function payBtnSubmit() {
        $('#paySubmitBtn').click();
    }

    function cartBtnSubmit() {
        $('#cartSubmitBtn').click();
    }
</script>

</body>
</html>